﻿using System;
using System.Collections.Generic;
using System.Web;
using Zadani09.db.ODS;

namespace Zadani09
{
    public partial class Umelci : System.Web.UI.Page
    {


        protected void Page_Init(object sender, EventArgs e)
        {
            GridView1.SelectedIndexChanged += new EventHandler(GridView1_SelectedIndexChanged);
        }

        void GridView1_SelectedIndexChanged(object sender, EventArgs e)
        {
            TextBox1.Text = ViewState["vyhl_umelce"] == null ? "" : (string)ViewState["vyhl_umelce"];
            Label2.Visible = true;
        }

        protected void Page_Load(object sender, EventArgs e)
        {

            if (GridView1.Rows.Count <= 0)
            {
                Label1.Text = "Nebyli nalezeni žádní umělci";
                Label2.Visible = false;
            }
            else
            {
                Label1.Text = string.Empty;
            }
        }

        protected override void OnPreRenderComplete(EventArgs e)
        {

            Session["vyhl_podle_umelce"] = IsPostBack ? DropDownList1.SelectedValue : DropDownList1.Items[0].Value;

            GridView1.DataBind();//nejspis nutne aby to chodilo na serveru
            //me to na localu chodi i bez toho..
            if (GridView1.Rows.Count <= 0)
            {
                Label1.Text = "<br/> Nebyli nalezeni žádní umělci.";
                schovejVyhledavani();
            }
            else
            {
                Label1.Text = string.Empty;
                zobrazVyhledavani();
            }

            Label2.Visible = GridView1.SelectedValue != null;

            base.OnPreRenderComplete(e);

        }

        [System.Web.Services.WebMethodAttribute(), System.Web.Script.Services.ScriptMethodAttribute()]
        public static string[] GetCompletionList(string prefixText, int count, string contextKey)
        {
            UmelecDataObject umelec = new UmelecDataObject();
            List<db.Umelec> vysl;
            List<string> vrat = new List<string>();

            vysl = umelec.cti_s_vyhledavanim(null, prefixText,
                        (string)HttpContext.Current.Session["vyhl_podle_umelce"]);

            foreach (db.Umelec jeden in vysl)
                vrat.Add(jeden[HttpContext.Current.Session["vyhl_podle_umelce"].ToString()].ToString());

            return (vrat.ToArray());
        }

        protected void Button1_Click(object sender, EventArgs e)
        {
            GridView1.DataBind();
            FormView1.DataBind();
            ViewState["vyhl_umelce"] = TextBox1.Text;
        }

        private void schovejVyhledavani()
        {
            if (UpdatePanel1 != null) UpdatePanel1.Visible = false;
            TextBox1.Visible = false;
            Button1.Visible = false;
            DropDownList1.Visible = false;
        }

        private void zobrazVyhledavani()
        {
            if (UpdatePanel1 != null) UpdatePanel1.Visible = true;
            TextBox1.Visible = true;
            Button1.Visible = true;
            DropDownList1.Visible = true;
        }
    }
}
